package cn.caskm.souvenirshopsystem.controller.api;

import cn.caskm.souvenirshopsystem.domain.Result;
import cn.caskm.souvenirshopsystem.domain.dto.PageDTO;
import cn.caskm.souvenirshopsystem.domain.query.UserCouponQuery;
import cn.caskm.souvenirshopsystem.domain.vo.CouponVO;
import cn.caskm.souvenirshopsystem.domain.vo.UserCouponVO;
import cn.caskm.souvenirshopsystem.service.CouponService;
import cn.caskm.souvenirshopsystem.service.UserCouponService;
import cn.caskm.souvenirshopsystem.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/coupon")
@RequiredArgsConstructor
public class CouponController {

    private final CouponService couponService;
    private final UserCouponService userCouponService;

    @GetMapping("/available")
    public Result<List<CouponVO>> getAvailableCoupons() {
        return couponService.getAvailableCoupons();
    }

    @PostMapping("/receive/{couponId}")
    public Result<Void> receiveCoupon(@PathVariable("couponId") Integer couponId) {
        return userCouponService.receiveCoupon(couponId);
    }

    @GetMapping("/myCoupons")
    public Result<PageDTO<UserCouponVO>> getMyCoupons(UserCouponQuery query) {
        Long userId = UserContext.getUser();
        query.setUserId(userId.intValue());
        return userCouponService.page(query);
    }

    @GetMapping("/myAvailableCoupons")
    public Result<List<UserCouponVO>> getMyAvailableCoupons(@RequestParam("orderAmount") BigDecimal orderAmount) {
        Long userId = UserContext.getUser();
        return userCouponService.getUserAvailableCoupons(userId.intValue(), orderAmount);
    }

}
